package com.qianfeng.taoyuan.volleyDemo;

import android.graphics.Bitmap;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.viewpagerindicator.TitlePageIndicator;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private RequestQueue queue;
    private String str = "http://www.0790tg.com/app/api.php?s=Tuan/goodsList&lng=116.412361&lat=39.966550&page=1&orderby=range";
    private String imgUrl = "http://i3.72g.com/upload/201507/201507171359292827.jpg";
    private String str2 = "http://zhushou.72g.com/app/game/game_list/";

    /**联网使用类Thread+handler AsyncTask HttpClient UrlConnection
     *volley框架功能 json数据加载 图片异步下载 缓存功能 跟Activity生命周期联动
     * 第一步创建请求队列
     *第二步创建请求
     *第三步设置Tag
     *第四步请求添加到消息队列
     *第五步取消请求
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        queue = Volley.newRequestQueue(this);
//        stringReuquestMedhod();
//        StringRequestPostMedhod();
//        imageReuquestMedhod();
//        netWorkImageViewRequestMethod();
        //Set the pager with an adapter
        ViewPager pager = (ViewPager)findViewById(R.id.pager);

        ImageView image1 = new ImageView(this);
        image1.setImageResource(R.mipmap.c1);
        ImageView image2 = new ImageView(this);
        image2.setImageResource(R.mipmap.c2);
        ImageView image3 = new ImageView(this);
        image3.setImageResource(R.mipmap.c4);
        ImageView image4 = new ImageView(this);
        image4.setImageResource(R.mipmap.c3);
        ImageView[] imageViews = new ImageView[]{image1,image2,image3,image4};
        pager.setAdapter(new TestAdapter(imageViews));
//        pager.setOnPageChangeListener(this);

       // Bind the title indicator to the adapter
        TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titles);
        titleIndicator.setViewPager(pager);
        titleIndicator.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(this,""+position,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

//    private void StringRequestPostMedhod() {
//        StringRequest request = new StringRequest(Request.Method.POST,str2,new Response.Listener<String>(){
//            @Override
//            public void onResponse(String response) {
//                ((TextView)findViewById(R.id.text_img)).setText(response);
//            }
//        },new Response.ErrorListener(){
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        }){
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                HashMap<String,String> map = new HashMap<>();
//                map.put("platform","1");
//                map.put("page","1");
//                return getUrlParams();
//            }
//        };
//        request.setTag(this);
//        queue.add(request);
//    }
//
//
//
//    private void imageReuquestMedhod() {
//        ImageRequest request = new ImageRequest(imgUrl,new Response.Listener<Bitmap>(){
//            @Override
//            public void onResponse(Bitmap response) {
//                ((ImageView)findViewById(R.id.image_request_img)).setImageBitmap(response);
//            }
//        },800,800, ImageView.ScaleType.CENTER_INSIDE,Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        request.setTag(this);
//        queue.add(request);
//    }
//
//    private Map<String, String> getUrlParams() {
//
//        return null;
//    }
//    private void netWorkImageViewRequestMethod(){
//        ImageLoader loader = new ImageLoader(queue,MyImageCache.getImageCacheInstance());
//        NetworkImageView networkImageView = (NetworkImageView) findViewById(R.id.netimage_request_img);
//        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
//        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
//        networkImageView.setImageUrl(imgUrl,loader);
//    }
//
//    private void stringReuquestMedhod() {
//
//        StringRequest request = new StringRequest(Request.Method.GET,str,new Response.Listener<String>(){
//            @Override
//            public void onResponse(String response) {
//                Toast.makeText(MainActivity.this,response,Toast.LENGTH_SHORT).show();
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        request.setTag(this);
//        queue.add(request);
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        queue.cancelAll(this);
//    }
}
